using System.Collections.Generic;
using System.Drawing;

namespace ICSharpCode.TextEditor
{
	public class BrushRegistry
	{
		private static Dictionary<Color, Brush> brushes = new Dictionary<Color, Brush>();

		private static Dictionary<Color, Pen> pens = new Dictionary<Color, Pen>();

		private static Dictionary<Color, Pen> dotPens = new Dictionary<Color, Pen>();

		private static readonly float[] dotPattern = new float[4] { 1f, 1f, 1f, 1f };

		public static Brush GetBrush(Color color)
		{
			lock (BrushRegistry.brushes)
			{
				if (!BrushRegistry.brushes.TryGetValue(color, out var value))
				{
					value = new SolidBrush(color);
					BrushRegistry.brushes.Add(color, value);
				}
				return value;
			}
		}

		public static Pen GetPen(Color color)
		{
			lock (BrushRegistry.pens)
			{
				if (!BrushRegistry.pens.TryGetValue(color, out var value))
				{
					value = new Pen(color);
					BrushRegistry.pens.Add(color, value);
				}
				return value;
			}
		}

		public static Pen GetDotPen(Color color)
		{
			lock (BrushRegistry.dotPens)
			{
				if (!BrushRegistry.dotPens.TryGetValue(color, out var value))
				{
					value = new Pen(color);
					value.DashPattern = BrushRegistry.dotPattern;
					BrushRegistry.dotPens.Add(color, value);
				}
				return value;
			}
		}
	}
}
